#include "wscdummyperson.h"
using namespace FsxWebService;

//
// Constructor
//
WscDummyPerson::WscDummyPerson(const int port, const QString &serverUrl, const QString &path, QObject *parent) :
    WscRequestHandler(port, serverUrl, path, parent)
{
    // constructor code
}

//
// Service of the web service
//
void WscDummyPerson::service(HttpRequest&, HttpResponse& response) {

    // set some headers
    response.setHeader("Content-Type", "application/json; charset=ISO-8859-1");
    response.setCookie(HttpCookie("WscDummyPerson","CreateDummyPerson",600));

    // generate response
    // http://qt-project.org/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa
    QString dp = WscDummyPerson::getDummyPerson();
    QByteArray ba = dp.toLocal8Bit();
    // const char *baChar = ba.data(); // force ba to stay in memory until it is written
    response.write(ba);
}

//
// Gives me a dummy person JSON string
//
QString WscDummyPerson::getDummyPerson() {

    QString dummyPerson =
            "{\n"
            "\"firstName\": \"John\",\n"
            "\"lastName\" : \"Smith\",\n"
            "\"age\"      : 25,\n"
            "\"address\"  :\n"
            "{\n"
            "    \"streetAddress\": \"21 2nd Street\",\n"
            "    \"city\"         : \"New York\",\n"
            "    \"state\"        : \"NY\",\n"
            "    \"postalCode\"   : \"10021\"\n"
            "},\n"
            "\"phoneNumber\":\n"
            "  [\n"
            "    {\n"
            "      \"type\"  : \"home\",\n"
            "      \"number\": \"212 555-1234\"\n"
            "    },\n"
            "    {\n"
            "      \"type\"  : \"fax\",\n"
            "      \"number\": \"646 555-4567\"\n"
            "    }\n"
            "  ]\n"
            "}\n";

    return dummyPerson;
}
